package com.cloudcreativity.blyg.utils;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.databinding.LayoutDialogEditProviderBinding;
import com.cloudcreativity.blyg.sqlite.Area;
import com.cloudcreativity.blyg.sqlite.City;
import com.cloudcreativity.blyg.sqlite.Provider;
import com.cloudcreativity.blyg.sqlite.Province;

public class EditProviderUtils extends Dialog {

    public ObservableField<String> name = new ObservableField<>();
    public ObservableField<String> username = new ObservableField<>();
    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<Province> province = new ObservableField<>();
    public ObservableField<City> city = new ObservableField<>();
    public ObservableField<Area> area = new ObservableField<>();
    public ObservableField<String> address = new ObservableField<>();
    private final LayoutDialogEditProviderBinding binding;
    private OnOkListener onOkListener;
    private Provider entity;
    public void setOnOkListener(OnOkListener onOkListener) {
        this.onOkListener = onOkListener;
    }

    public EditProviderUtils(@NonNull final Context context,Provider provider) {
        super(context,R.style.myProgressDialogStyle);
        assert getWindow() != null;
        KeyBoardHelper.notAutoFocus(getWindow());
        setCanceledOnTouchOutside(false);
        setCancelable(true);
        binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.layout_dialog_edit_provider,
                null,
                false);
        setContentView(binding.getRoot());
        binding.setUtils(this);
        this.entity = provider;
        name.set(provider.getName());
        username.set(provider.getContactMan());
        phone.set(provider.getContactMobile());
        province.set(provider.getProvince());
        city.set(provider.getCity());
        area.set(provider.getArea());
        address.set(provider.getAddressDetail());
        binding.tvArea.setText(province.get().getName()+"/"+city.get().getName()+"/"+area.get().getName());
    }

    public void onOk(){
        if(TextUtils.isEmpty(name.get())){
            ToastUtils.showShortToast(getContext(),"名称不能为空");
            return;
        }
        if(TextUtils.isEmpty(username.get())){
            ToastUtils.showShortToast(getContext(),"用户姓名不能为空");
            return;
        }
        if(TextUtils.isEmpty(phone.get())){
            ToastUtils.showShortToast(getContext(),"电话不能为空");
            return;
        }
        if(province.get()==null){
            ToastUtils.showShortToast(getContext(),"省不能为空");
            return;
        }
        if(city.get()==null){
            ToastUtils.showShortToast(getContext(),"市不能为空");
            return;
        }
        if(area.get()==null){
            ToastUtils.showShortToast(getContext(),"区不能为空");
            return;
        }
        if(onOkListener!=null){
            Provider provider = new Provider(entity.getId(),name.get(),
                    province.get().getId(),
                    city.get().getId(),
                    area.get().getId(),
                    address.get(),
                    username.get(),phone.get(),null,
                    null,null,
                    entity.getCreateTime(),DateUtils.getSimpleDateTimeStr());

            onOkListener.onOk(provider);
        }
        KeyBoardHelper.close(getContext(),this);
        dismiss();
    }

    public interface OnOkListener{
        void onOk(Provider provider);
    }

    public void onClose(){
        KeyBoardHelper.close(getContext(),this);
        dismiss();
    }

    public void onClear(){
        name.set(null);
        username.set(null);
        phone.set(null);
        province.set(null);
        city.set(null);
        area.set(null);
        address.set(null);
        binding.tvArea.setText(null);
    }

    public void onAreaChoose(){
        AreaChooseUtils areaChooseUtils = new AreaChooseUtils(getContext());
        areaChooseUtils.show();
        areaChooseUtils.setOnOkListener(new AreaChooseUtils.OnOkListener() {
            @Override
            public void onOk(Province p, City c, Area a) {
                province.set(p);
                city.set(c);
                area.set(a);
                binding.tvArea.setText(province.get().getName()+"/"+city.get().getName()+"/"+area.get().getName());
            }
        });
    }
}
